<?php
class My_Service_Soap
{
    protected $_client;

    public function getClient($config)
    {
        //check cache instance
        if($this->_client instanceof Zend_Soap_Client){
            return $this->_client;
        }

        //check input
        if( !isset($config->wsdl[0]) ||
            !isset($config->function[0])){
            return null;
        }

        //init service connection
        try{
            $this->_client = new Zend_Soap_Client($config->wsdl);
            return $this->_client;
        }catch (SoapFault $e) {
            //write log here
            //TODO
            return null;
        }catch(Excetion $ex){
            //write log here
            //TODO
            return null;
        }
    }

    // prepareParams
    public function prepareParams($config)
    {
        $parameters = array();
        $parameters['argument0'] = isset($config['agentId'][0]) ? $config['agentId'] : '';
        $parameters['argument1'] = isset($config['agentKey'][0]) ? $config['agentKey'] : '';
        $parameters['argument2'] = isset($config['index'][0]) ? $config['index'] : '';
        $dateFormat = isset($config['date']['format'][0]) ? $config['date']['format'] : My_Common::DATE_FORMAT_DEFAULT;
        $parameters['argument3'] = isset($config['date']['value']) ? $config['date']['value'] : My_Common::getYesterdayTime(My_Common::DATE_TYPE_STRING, $dateFormat);
        $parameters['argument4'] = isset($config['dateEnd']['value']) ? $config['dateEnd']['value'] : My_Common::getYesterdayTime(My_Common::DATE_TYPE_STRING, $dateFormat);
        $parameters['argument5'] = isset($config['serverList']) ? $config['serverList'] : '';
        $secret = isset($config['secret'][0]) ? $config['secret'] : '';
        $signature = md5($parameters['argument1'] . md5($parameters['argument2']) . $secret);
        $parameters['argument6'] = $signature;

        return $parameters;
    }

    // prepareParams KPI SSGroup
    public function prepareParams1($config)
    {
        $parameters = array();
        $parameters['argument0'] = isset($config['agentId'][0]) ? $config['agentId'] : '';
        $parameters['argument1'] = isset($config['agentKey'][0]) ? $config['agentKey'] : '';
        $parameters['argument2'] = isset($config['index'][0]) ? $config['index'] : '';
        $dateFormat = isset($config['date']['format'][0]) ? $config['date']['format'] : My_Common::DATE_FORMAT_DEFAULT;
        $parameters['argument3'] = isset($config['date']['value']) ? $config['date']['value'] : My_Common::getYesterdayTime(My_Common::DATE_TYPE_STRING, $dateFormat);
        $parameters['argument4'] = isset($config['dateEnd']['value']) ? $config['dateEnd']['value'] : My_Common::getYesterdayTime(My_Common::DATE_TYPE_STRING, $dateFormat);
        $secret = isset($config['secret'][0]) ? $config['secret'] : '';
        $signature = md5($parameters['argument1'] . md5($parameters['argument2']) . $secret);
        $parameters['argument5'] = $signature;

        return $parameters;
    }
    // prepareParams KPI Game
    public function prepareParams2($config)
    {
        $parameters = array();
        $parameters['argument0'] = isset($config['agentId'][0]) ? $config['agentId'] : '';
        $parameters['argument1'] = isset($config['agentKey'][0]) ? $config['agentKey'] : '';
        $parameters['argument2'] = isset($config['index'][0]) ? $config['index'] : '';
        $dateFormat = isset($config['date']['format'][0]) ? $config['date']['format'] : My_Common::DATE_FORMAT_DEFAULT;
        $parameters['argument3'] = isset($config['date']['value']) ? $config['date']['value'] : My_Common::getYesterdayTime(My_Common::DATE_TYPE_STRING, $dateFormat);
        $parameters['argument4'] = isset($config['dateEnd']['value']) ? $config['dateEnd']['value'] : My_Common::getYesterdayTime(My_Common::DATE_TYPE_STRING, $dateFormat);
        $parameters['argument5'] = isset($config['channelingAgentId'][0]) ? $config['channelingAgentId'] : '';
        $parameters['argument6'] = isset($config['serverList']) ? $config['serverList'] : '';
        $secret = isset($config['secret'][0]) ? $config['secret'] : '';
        $signature = md5($parameters['argument1'] . md5($parameters['argument2']) . $secret);
        $parameters['argument7'] = $signature;

        return $parameters;
    }
    // prepareParams CCU ss14
    public function prepareParamsCCU($config)
    {
        $parameters = array();
        $parameters['argument0'] = isset($config['agentId'][0]) ? $config['agentId'] : '';
        $parameters['argument1'] = isset($config['agentKey'][0]) ? $config['agentKey'] : '';
        $parameters['argument2'] = isset($config['date']['startTime']) ? $config['date']['startTime'] : ''; 
        $parameters['argument3'] = isset($config['date']['endTime']) ? $config['date']['endTime'] : ''; 
        $parameters['argument4'] = isset($config['serverList']) ? $config['serverList'] : '';
        $parameters['argument5'] = isset($config['recordNumber']) ? $config['recordNumber'] : '';
        $secret = isset($config['secret']) ? $config['secret'] : '';
        $signature = md5($parameters['argument1'] . md5($parameters['argument2']) . $secret);
        $parameters['argument6'] = $signature;

        return $parameters;
    }
    // prepareParams CCU ss14
    public function prepareParamsCCUSS20($config)
    {
        $parameters = array();
        $parameters['argument0'] = isset($config['agentId'][0]) ? $config['agentId'] : '';
        $parameters['argument1'] = isset($config['agentKey'][0]) ? $config['agentKey'] : '';
        $parameters['argument2'] = isset($config['date']['startTime']) ? $config['date']['startTime'] : ''; 
        $parameters['argument3'] = isset($config['date']['endTime']) ? $config['date']['endTime'] : ''; 
        $parameters['argument4'] = isset($config['serverList']) ? $config['serverList'] : '';
        $parameters['argument5'] = isset($config['channelingList']) ? $config['channelingList'] : '';
        $parameters['argument6'] = isset($config['serverChannelingList']) ? $config['serverChannelingList'] : '';
        $parameters['argument7'] = isset($config['recordNumber']) ? $config['recordNumber'] : '';
        $secret = isset($config['secret']) ? $config['secret'] : '';
        $signature = md5($parameters['argument1'] . md5($parameters['argument2']) . $secret);
        $parameters['argument8'] = $signature;

        return $parameters;
    }
    // prepareParams transaction ss13
    public function prepareParams3($config)
    {
        $parameters = array();
        $parameters['argument0'] = isset($config['agentId'][0]) ? $config['agentId'] : '';
        $parameters['argument1'] = isset($config['agentKey'][0]) ? $config['agentKey'] : '';
        $parameters['argument2'] = isset($config['startDate'][0]) ? $config['startDate'] : '';
        $parameters['argument3'] = isset($config['dateEnd'][0]) ? $config['dateEnd'] : '';
        $parameters['argument4'] = isset($config['username'][0]) ? $config['username'] : '';
        $parameters['argument5'] = isset($config['serverId'][0]) ? $config['serverId'] : '';
        $parameters['argument6'] = isset($config['status']) ? $config['status'] : '';
        $parameters['argument7'] = isset($config['channelingAgentId'][0]) ? $config['channelingAgentId'] : '';
        $secret = isset($config['secret'][0]) ? $config['secret'] : '';                
        $parameters['argument8'] = md5($parameters['argument1'] . md5($parameters['argument2'] . $parameters['argument3']) . $secret);

        return $parameters;
    }
    // prepareParams transaction sg0-sg4, ss17, ss17bds
    public function prepareParams4($config)
    {
        $parameters = array();
        $parameters['argument0'] = isset($config['agentId'][0]) ? $config['agentId'] : '';
        $parameters['argument1'] = isset($config['agentKey'][0]) ? $config['agentKey'] : '';
        $parameters['argument2'] = isset($config['startDate'][0]) ? $config['startDate'] : '';
        $parameters['argument3'] = isset($config['dateEnd'][0]) ? $config['dateEnd'] : '';
        $parameters['argument4'] = isset($config['username'][0]) ? $config['username'] : '';
        $parameters['argument5'] = isset($config['serverId'][0]) ? $config['serverId'] : '';
        $parameters['argument6'] = isset($config['status']) ? $config['status'] : '';
        $secret = isset($config['secret'][0]) ? $config['secret'] : '';                
        $parameters['argument7'] = md5($parameters['argument1'] . md5($parameters['argument2'] . $parameters['argument3']) . $secret);
        return $parameters;
    }
    // prepareParams card revenue (use in mobile app)
    public function prepareParams5($config)
    {
        $parameters = array();
        $parameters['argument0'] = isset($config['agentId'][0]) ? $config['agentId'] : '';
        $parameters['argument1'] = isset($config['agentKey'][0]) ? $config['agentKey'] : '';
        $parameters['argument2'] = isset($config['index'][0]) ? $config['index'] : '';
        $parameters['argument3'] = isset($config['date']['value']) ? $config['date']['value'] : '';
        $parameters['argument4'] = isset($config['dateEnd']['value']) ? $config['dateEnd']['value'] : '';
        $parameters['argument5'] = isset($config['channelingAgentId'][0]) ? $config['channelingAgentId'] : '';
        $secret = isset($config['secret'][0]) ? $config['secret'] : '';                
        $parameters['argument6'] = md5($parameters['argument1'] . md5($parameters['argument2'] . $parameters['argument3'] . $parameters['argument4'] . $parameters['argument5']) . $secret);

        return $parameters;
    }
}